package com.longma.server.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.longma.server.constant.enums.ResponseCode;
import com.longma.server.dao.InspectionPlanDAO;
import com.longma.server.entity.InspectionPlan;
import com.longma.server.exception.BusinessException;
import com.longma.server.pojo.dto.*;
import com.longma.server.service.IInspectionPlanService;
import com.longma.server.util.MybatisPlusPageUtil;
import com.longma.server.util.WrappersUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HAIBO
 * @since 2021-04-28
 */
@Service
public class InspectionPlanServiceImpl implements IInspectionPlanService {
    @Autowired
    InspectionPlanDAO inspectionPlanDAO;

    /**
     * 新增巡視計劃
     */
    @Override
    public InspectionPlan addInspectionPlan(InspectionPlanDTO addRequestDTO) {
        InspectionPlan inspectionPlan = new InspectionPlan();
        BeanUtil.copyProperties(addRequestDTO,inspectionPlan);
        inspectionPlanDAO.save(inspectionPlan);
        return inspectionPlan;
    }

    /**
     * 根据巡視id删除巡視計劃
     */
    @Override
    public boolean deleteInspectionPlanById(Integer id) {
        if (!isExistId(id)){
            throw new BusinessException(ResponseCode.PARAM_NOT_VALID.setMessage("id不存在"));
        }
        return inspectionPlanDAO.removeById(id);
    }
    /**
     * 根据巡視记录id列表删除巡視計劃
     */
    @Override
    public boolean deleteInspectionPlanByIds(List<Integer> ids) {
        if (!isExistIds(ids)){
            throw new BusinessException(ResponseCode.PARAM_NOT_VALID.setMessage("id不存在"));
        }
        return inspectionPlanDAO.removeByIds(ids);
    }
    /**
     * 分页查询 获取巡視計劃
     */
    @Override
    public IPage<InspectionPlan> getInspectionPlanByPage(Long pageNum, Long pageSize, Boolean all, List<AdvanceQueryConditionDTO> conditions) {
        LambdaQueryWrapper<InspectionPlan> wrapper = WrappersUtil.lambdaQuery(conditions, InspectionPlan.class);
        Page<InspectionPlan> pagePlan = MybatisPlusPageUtil.getPage(pageNum, pageSize, all);
        return inspectionPlanDAO.page(pagePlan, wrapper);
    }

    /**
     * 更新巡視計劃
     */
    @Override
    public InspectionPlan updateInspectionPlan(InspectionPlanUpdateDTO updateRequestDTO){

        // 存在校验
        if(inspectionPlanDAO.getById(updateRequestDTO.getId()) == null){
            throw new BusinessException(ResponseCode.PARAM_NOT_VALID.setMessage("id不存在"));
        }

        // 复制
        InspectionPlan inspectionPlan = new InspectionPlan();
        BeanUtil.copyProperties(updateRequestDTO,inspectionPlan);
        inspectionPlanDAO.updateById(inspectionPlan);
        return inspectionPlan;
    }

    /**
     * 判断id列表是否存在
     */
    private boolean isExistIds(List<Integer> ids) {
        if (CollUtil.isEmpty(ids)) {
            return false;
        }
        LambdaQueryWrapper<InspectionPlan> wrapper = Wrappers.lambdaQuery();
        Set<Integer> productIds = new HashSet<>(ids);
        return inspectionPlanDAO.count(wrapper.in(InspectionPlan::getId, productIds)) == productIds.size();
    }

    /**
     * 判断id是否存在
     */
    private boolean isExistId(Integer id) {
        return isExistIds(Arrays.asList(id));
    }
}
